<?php
/** 
 * @uses 		服务model
 * @author 		Duan
 * @version 	1.0
 * @copyright	Yihuan							  
 */

namespace Common\Model;

class ServeModel 
{

	protected static $_serve = []; 	//静态化服务	

	/**
	 * @uses 	获取redis服务
	 * @return 	object
	 */
	public static function serveRedis()
	{
		if (self::$_serve['redis']) {
			return self::$_serve['redis'];
		}
		$redis  = self::connectRedis();
		if (!$redis) {
			$error  	= "时间：".date('Ymd H:i:s')." ， 错误信息：不能连接Redis！\n";
			writeLog(APP_LOG_PATH.'error/' , 'error_'.date('Ymd_His') , $error);
			echo json_encode(['code' => 301]); die;
		}
		self::$_serve['redis'] 	= $redis;
		return self::$_serve['redis'];

	}
	/**
	 * @uses 	链接redis服务
	 * @return 	object
	 */
	protected static function connectRedis() 
	{
		if (!class_exists('Redis')) {
			$error  	= "时间：".date('Ymd H:i:s')." ， 错误信息：请安装Redis扩展！\n";
			writeLog(APP_LOG_PATH.'error/' , 'error_'.date('Ymd_His') , $error);
			echo json_encode(['code' => 302]); die;
		}
		$redis 	= new \Redis();
		$config = C('REDIS_CONFIG');
		$port 	= $config['port'];
		$host 	= $config['host'];
		$auth 	= $config['auth'];
		if ($redis->connect($host , $port) == false) { 
			return null;
		}
		if (!$auth) { 
			return $redis;
		}
		if ($redis->auth($auth) == false) {
			return null;
		}
		return $redis;
	}
}